Skip to content

feat: allow overriding headers per request#617

Merged
rhamzeh merged 6 commits intomainfrom
feat/go-per-request-headers
Oct 7, 2025
Merged

feat: allow overriding headers per request#617
rhamzeh merged 6 commits intomainfrom
feat/go-per-request-headers

Conversation

@rhamzeh
Copy link
Copy Markdown
Member

@rhamzeh rhamzeh commented Sep 15, 2025

Description

This PR adds support for sending custom headers per request.

It is complete, however it's kept in draft as it will have many conflicts with the other Go SDK PRs that will need to be resolved once the others are merged.

Merge after #616

What problem is being solved?

How is it being solved?

What changes are made to solve it?

References

addresses go sdk part of #569

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

Summary by CodeRabbit

  • New Features

    • Go client now supports default headers and per-request custom headers, with per-request values overriding defaults.
  • Bug Fixes

    • Default headers no longer overwrite preexisting request headers; existing values are preserved.
  • Documentation

    • Updated initialization guide with examples for setting default and per-request headers.
    • Added example demonstrating a Check call with custom headers.
  • Tests

    • Extensive test coverage added to verify header merging and precedence across client operations.
  • Chores

    • Changelog updated with the unreleased entry for custom header support.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Sep 15, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Adds per-request custom header support to the Go SDK: introduces RequestOptions with Headers, threads options through generated API request builders and client option structs, adjusts default header application to avoid overwriting existing headers, updates docs and example, and adds comprehensive tests for header merging and precedence.

Changes

Cohort / File(s) Summary
Changelog
config/clients/go/CHANGELOG.md.mustache
Adds unreleased entry noting support for per-request custom headers and prior contextual tuples feature.
Config overrides
config/clients/go/config.overrides.json
Maps two new test files (client/client_headers_test.go, api_headers_test.go) with destination filenames unchanged.
Docs
config/clients/go/template/README_initializing.mustache
Documents DefaultHeaders and per-request Headers via Options, with usage examples and override semantics.
Generated API request options
config/clients/go/template/api.mustache
Adds RequestOptions { Headers map[string]string }; adds options field and Options(...) setter on Api{{operationId}}Request; applies r.options.Headers to header params during Execute.
HTTP request preparation
config/clients/go/template/api_client.mustache
Changes prepareRequest to set default headers only when absent, preserving existing values; no public API changes.
Client surface embedding options
config/clients/go/template/client/client.mustache
Introduces public alias type RequestOptions = fgaSdk.RequestOptions; embeds RequestOptions into many public option structs; updates execution paths to pass .Options(requestOptions) into SDK calls.
API header tests
config/clients/go/template/api_headers_test.go
Adds tests validating header merging/precedence across many endpoints using mock server; verifies default vs per-request behavior.
Client header tests
config/clients/go/template/client/client_headers_test.go
Adds client-level tests covering header propagation and override scenarios across numerous methods; includes helper setup.
Example update
config/clients/go/template/example/example1/example1.go
Adds example Check call using Options with custom Headers and prints result.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App as Application
  participant Client as Go Client (high-level)
  participant SDK as Generated API Request
  participant Core as prepareRequest
  participant HTTP as HTTP Server

  App->>Client: Invoke Method(opts with RequestOptions.Headers)
  Client->>SDK: Build Api{{operation}}Request\n.Options(requestOptions).Body(...).Execute()
  SDK->>Core: prepareRequest(ctx, method, path, headers, body)
  Note over Core: 1) Start with local headers<br/>2) Apply per-request Headers<br/>3) Apply DefaultHeaders only if absent
  Core->>HTTP: HTTP request with merged headers
  HTTP-->>Core: Response
  Core-->>SDK: Response
  SDK-->>Client: Parsed result
  Client-->>App: Return result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested labels

documentation

Suggested reviewers

  • ewanharris

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 21.88% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly conveys the primary change of allowing request-specific header overrides in the Go SDK and aligns with conventional commit syntax without extraneous details. It is concise, clear, and fully related to the changes introduced. Teammates scanning history will immediately understand the main feature added.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@rhamzeh rhamzeh marked this pull request as ready for review October 6, 2025 20:11
@rhamzeh rhamzeh requested a review from a team as a code owner October 6, 2025 20:11
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
config/clients/go/template/api_headers_test.go (1)

261-287: Return a BatchCheckResponse-shaped body for accuracy

The mock returns {"test-correlation-id": {...}}. The API response is {"result": {...}}. Returning the documented shape reduces future fragility.

Apply:

- server := createAPITestServer(t, &capturedHeaders, `{"test-correlation-id": {"allowed": true}}`)
+ server := createAPITestServer(t, &capturedHeaders, `{"result": {"test-correlation-id": {"allowed": true}}}`)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4c438 and 0c6f157.

📒 Files selected for processing (9)
  • config/clients/go/CHANGELOG.md.mustache (1 hunks)
  • config/clients/go/config.overrides.json (1 hunks)
  • config/clients/go/template/README_initializing.mustache (1 hunks)
  • config/clients/go/template/api.mustache (4 hunks)
  • config/clients/go/template/api_client.mustache (1 hunks)
  • config/clients/go/template/api_headers_test.go (1 hunks)
  • config/clients/go/template/client/client.mustache (48 hunks)
  • config/clients/go/template/client/client_headers_test.go (1 hunks)
  • config/clients/go/template/example/example1/example1.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
config/**/*.mustache

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Validate mustache syntax and variable references across all template files, including CHANGELOG.md.mustache

Files:

  • config/clients/go/template/api.mustache
  • config/clients/go/template/api_client.mustache
  • config/clients/go/template/README_initializing.mustache
  • config/clients/go/CHANGELOG.md.mustache
  • config/clients/go/template/client/client.mustache
config/**/*.{json,mustache}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Never hardcode API keys or credentials in configuration or template files

Files:

  • config/clients/go/template/api.mustache
  • config/clients/go/config.overrides.json
  • config/clients/go/template/api_client.mustache
  • config/clients/go/template/README_initializing.mustache
  • config/clients/go/CHANGELOG.md.mustache
  • config/clients/go/template/client/client.mustache
config/clients/*/config.overrides.json

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

config/clients/*/config.overrides.json: Always update the packageVersion in each language-specific config.overrides.json when making version changes
Maintain FOSSA compliance notice IDs in each language’s config.overrides.json

Files:

  • config/clients/go/config.overrides.json
config/{common/config.base.json,clients/*/config.overrides.json}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Ensure consistent versioning across base and language override configuration files to avoid version conflicts

Files:

  • config/clients/go/config.overrides.json
🔇 Additional comments (8)
config/clients/go/CHANGELOG.md.mustache (1)

5-5: Changelog entry captures the feature well

Nice to see the per-request headers support called out so it’s easy to surface in release notes.

config/clients/go/config.overrides.json (1)

48-53: Thank you for wiring the new tests into the generator

Adding these file mappings guarantees the header-focused test suites land in the generated SDK.

config/clients/go/template/api.mustache (2)

27-80: RequestOptions integration looks solid

Defining the reusable RequestOptions type and threading it through the request struct plus fluent setter keeps the API ergonomic while enabling per-call header overrides.


333-336: Great call on merging override headers right before dispatch

Applying the per-request headers here guarantees they take precedence without disturbing earlier header assembly logic.

config/clients/go/template/README_initializing.mustache (1)

118-155: Docs clearly explain both default and per-request header flows

The examples make it straightforward for users to adopt the new Options-based overrides right away.

config/clients/go/template/api_client.mustache (1)

292-295: Thanks for preserving caller-specified headers

Guarding the default header assignment prevents client defaults from clobbering explicit per-request values—exactly what the new feature needs.

config/clients/go/template/example/example1/example1.go (1)

257-274: Good example usage of per-request headers

The Check example with Options(RequestOptions{Headers: ...}) is correct and aligns with the new API.

config/clients/go/template/client/client.mustache (1)

683-701: RequestOptions propagation looks solid

Passing request-scoped options via .Options(requestOptions) across client methods is consistent and correct.

Also applies to: 760-779, 839-857, 919-953, 1025-1045, 1140-1153, 1216-1241, 1320-1358, 1438-1474, 1668-1673, 2174-2183, 2198-2204, 2376-2380, 2560-2586, 2679-2716, 2980-2990, 3075-3083, 3216-3225

sergiught
sergiught previously approved these changes Oct 7, 2025
jimmyjames
jimmyjames previously approved these changes Oct 7, 2025
sergiught
sergiught previously approved these changes Oct 7, 2025
@rhamzeh rhamzeh dismissed stale reviews from sergiught and jimmyjames via 296135b October 7, 2025 14:16
@rhamzeh rhamzeh enabled auto-merge October 7, 2025 14:26
@rhamzeh rhamzeh added this pull request to the merge queue Oct 7, 2025
Merged via the queue into main with commit 591cc30 Oct 7, 2025
19 checks passed
@rhamzeh rhamzeh deleted the feat/go-per-request-headers branch October 7, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants